﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frmSongjian : Form
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        string tbs0, tbs1, tbs2, tbs3, tbs4, tbs5, tbs6, tbs7, tbs8, tbs9;
        string tbn1, tbn2, tbn3, tbn4, tbn5, tbn6, tbn7, tbn8, tbn9;
        string Songjian;
        bool b量具Search, b量具Insert, b量具Edit, b量具Del;
        public frmSongjian(string Songjian, string tbs0, string tbs1, string tbs2, string tbs3, string tbs4, string tbs5, string tbs6, string tbs7, string tbs8, string tbs9, string tbn1, string tbn2, string tbn3, string tbn4, string tbn5, string tbn6, string tbn7, string tbn8, string tbn9, bool b量具Search, bool b量具Insert, bool b量具Edit, bool b量具Del)
        {
            InitializeComponent();

            this.Songjian = Songjian;
            this.tbs0 = tbs0;
            this.tbs1 = tbs1;
            this.tbs2 = tbs2;
            this.tbs3 = tbs3;
            this.tbs4 = tbs4;
            this.tbs5 = tbs5;
            this.tbs6 = tbs6;
            this.tbs7 = tbs7;
            this.tbs8 = tbs8;
            this.tbs9 = tbs9;
            this.tbn1 = tbn1;
            this.tbn2 = tbn2;
            this.tbn3 = tbn3;
            this.tbn4 = tbn4;
            this.tbn5 = tbn5;
            this.tbn6 = tbn6;
            this.tbn7 = tbn7;
            this.tbn8 = tbn8;
            this.tbn9 = tbn9;
            this.b量具Search = b量具Search;
            this.b量具Insert = b量具Insert;
            this.b量具Edit = b量具Edit;
            this.b量具Del = b量具Del;
        }

        private void frmSongjian_Load(object sender, EventArgs e)
        {
            txt量具狀態.Enabled = false;
            txt量具狀態.Text = "送檢";

            // goi du lieu lay san pham dung
            if (Songjian == "1")
            {
                btn_Save.Text = "存檔";
                lb送檢.Text = "量具送檢";
                txt_量具編號.Text = tbs1;//tb1 0
                lbName量具編號.Text = tbs4;
                txt_規格型式.Text = tbs2;//cb1 1
                txt測量精度.Text = tbs3; //tb3 2
                txt_Name量具.Text = tbs4; //tb4 3
                txt_測量範圍.Text = tbs5; //cb2  4
                txt廠牌.Text = tbs7;
                txt校正類別.Text = tbs8;
                txt出廠日期.Text = tbs9;
            }
            // goi du lieu Tra san pham ve kho
            if (Songjian == "2")
            {
                lb送檢.Text = "量具檢定";
                btn_Save.Text = "返庫";
                hiden();
                txt_量具編號.Text = tbs1;//tb1 0
                txt量具狀態.Text = tbs6;//cb1 1
                txt_Name量具.Text = tbs2; //tb4 3
                txt_規格型式.Text = tbs3; //tb3 2
                txt數量.Text = "";
                txt廠牌.Text = tbs5; //cb2  4

                txt測量精度.Text = tbs7;
                txt_測量範圍.Text = tbs8;
                txt出廠日期.Text = tbs9;
                txt校正類別.Text = tbn1;
                txt_Date送檢.Text = tbn2;
                txt_Date檢定.Text = tbn3;
                txt_檢定機構.Text = tbn4;
                txt_檢定結果.Text = tbn5;
                txt_Date有效.Text = tbn6;
                txt_檢定費用.Text = tbn7;
                txt_辦理人員.Text = tbn8;
                txt_note.Text = tbn9;
            }
        }

        public void hiden()
        {
            btn修理紀錄.Enabled = false;
            txt_量具編號.Enabled = false;//tb1 0
            txt量具狀態.Enabled = false;//cb1 1
            txt_Name量具.Enabled = false; //tb4 3
            txt_規格型式.Enabled = false; //tb3 2
            txt數量.Enabled = true;
            txt廠牌.Enabled = false; //cb2  4

            txt測量精度.Enabled = false;
            txt_測量範圍.Enabled = false;
            txt出廠日期.Enabled = false;
            txt校正類別.Enabled = false;
            txt_Date送檢.Enabled = false;
            txt_Date檢定.Enabled = true;
            txt_檢定機構.Enabled = false;
            txt_檢定結果.Enabled = false;
            txt_Date有效.Enabled = true;
            txt_檢定費用.Enabled = true;
            txt_辦理人員.Enabled = true;
            txt_note.Enabled = true;
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                // lay san pham su dung
                if (Songjian == "1")
                {
                    if (txt_量具編號.Text == "")
                    {
                        MessageBox.Show("請您輸入量具編號", "錯誤", MessageBoxButtons.OK);
                        txt_量具編號.Focus();
                        return;
                    }
                    if (txt量具狀態.Text == "")
                    {
                        MessageBox.Show("請您輸入量具狀態", "錯誤", MessageBoxButtons.OK);
                        txt量具狀態.Focus();
                        return;
                    }
                    if (txt數量.Text == "")
                    {
                        MessageBox.Show("請您輸入送檢數量", "錯誤", MessageBoxButtons.OK);
                        txt數量.Focus();
                        return;
                    }
                    if (txt_Date送檢.Text == "")
                    {
                        MessageBox.Show("請您輸入送檢日期", "錯誤", MessageBoxButtons.OK);
                        txt_Date送檢.Focus();
                        return;
                    }
                    if (txt_Date檢定.Text == "")
                    {
                        MessageBox.Show("請您輸入檢定日期", "錯誤", MessageBoxButtons.OK);
                        txt_Date檢定.Focus();
                        return;
                    }

                    if (txt_Date有效.Text == "")
                    {
                        MessageBox.Show("請您輸入有效日期", "錯誤", MessageBoxButtons.OK);
                        txt_Date有效.Focus();
                        return;
                    }

                    if (txt_辦理人員.Text == "")
                    {
                        MessageBox.Show("請您輸入辦理人員", "錯誤", MessageBoxButtons.OK);
                        txt_辦理人員.Focus();
                        return;
                    }

                    if (txt_檢定機構.Text == "")
                    {
                        MessageBox.Show("請您輸入檢定機構", "錯誤", MessageBoxButtons.OK);
                        txt_檢定機構.Focus();
                        return;
                    }

                    if (txt_檢定結果.Text == "")
                    {
                        MessageBox.Show("請您輸入檢定結果", "錯誤", MessageBoxButtons.OK);
                        txt_檢定結果.Focus();
                        return;
                    }

                    if (txt_檢定費用.Text == "")
                    {
                        MessageBox.Show("請您輸入檢定費用", "錯誤", MessageBoxButtons.OK);
                        txt_檢定費用.Focus();
                        return;
                    }
                    // kiem tra thoi gian
                    DateTime a = DateTime.Parse(txt_Date檢定.Text);
                    DateTime b = DateTime.Parse(txt_Date送檢.Text);
                    if (a < b)
                    {
                        MessageBox.Show("請您輸入檢定日期 >= 送檢日期", "錯誤", MessageBoxButtons.OK);
                        txt_Date檢定.Focus();
                        return;
                    }
                    // kiem tra nhap shuliang nho hon so luong trong kho
                    SqlCommand sqlcmd = new SqlCommand();
                    sqlcmd.Connection = con;
                    con.Open();
                    sqlcmd.CommandText = "select 数量 from 量具訊息 where 量具編號=" + "N'" + tbs1 + "'";
                    sqlcmd.CommandType = CommandType.Text;
                    var icount = sqlcmd.ExecuteScalar();
                    int inumdel;
                    bool k = int.TryParse(txt數量.Text, out inumdel);
                    int check = int.Parse(txt數量.Text);
                    int istocknumber = int.Parse(icount.ToString());
                    if (!k || inumdel > int.Parse(icount.ToString()))
                    {
                        MessageBox.Show("請您輸入送檢數量量具要刪除(int,<" + istocknumber + ")", "錯誤", MessageBoxButtons.OK);
                        txt數量.Focus();
                        //return;
                    }
                    else if (istocknumber == 0 || check <=0)
                    {
                        MessageBox.Show("不能送檢", "錯誤", MessageBoxButtons.OK);
                        }
                    else { 
                    // tien hanh inset du lieu vao bang lingyong
                    DialogResult answer = MessageBox.Show("您確定要出入" + int.Parse(txt數量.Text) + "量具嗎？", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        //automation inset
                        string sp_insert = "sp_送檢_新增";
                        SqlCommand cmd = new SqlCommand(sp_insert, con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@量具編號", txt_量具編號.Text);
                        cmd.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text);
                        cmd.Parameters.AddWithValue("@量具名稱", txt_Name量具.Text);
                        cmd.Parameters.AddWithValue("@規格型號", txt_規格型式.Text);
                        cmd.Parameters.AddWithValue("@數量", Int32.Parse(txt數量.Text));
                        cmd.Parameters.AddWithValue("@廠牌代號", txt廠牌.Text.ToString());
                        cmd.Parameters.AddWithValue("@測量精度", txt測量精度.Text.ToString());
                        cmd.Parameters.AddWithValue("@測量範圍", txt_測量範圍.Text.ToString());
                        cmd.Parameters.AddWithValue("@出廠日期", DateTime.Parse(txt出廠日期.Text));
                        cmd.Parameters.AddWithValue("@校正類別代號", txt校正類別.Text);
                        cmd.Parameters.AddWithValue("@送檢日期", DateTime.Parse(txt_Date送檢.Text));
                        cmd.Parameters.AddWithValue("@檢定日期", DateTime.Parse(txt_Date檢定.Text));
                        cmd.Parameters.AddWithValue("@檢定機構代號", txt_檢定機構.Text.ToString());
                        cmd.Parameters.AddWithValue("@檢定結果", txt_檢定結果.Text);
                        cmd.Parameters.AddWithValue("@有效日期", DateTime.Parse(txt_Date有效.Text));
                        cmd.Parameters.AddWithValue("@檢定費用", txt_檢定費用.Text.ToString());
                        cmd.Parameters.AddWithValue("@人員代號", txt_辦理人員.Text.ToString());
                        cmd.Parameters.AddWithValue("@備註", txt_note.Text);
                        cmd.ExecuteNonQuery();

                        //ling yong liang ju
                        string delete = tbs1;
                        //string delete1 = tbl6;
                        string sp_increment = "sp_量具訊息_更新_-n";// tru so luong san pham trong kho khi linh yong
                        SqlCommand sqlincrement = new SqlCommand();
                        sqlincrement.Connection = con;
                        sqlincrement.CommandType = CommandType.StoredProcedure;
                        sqlincrement.CommandText = sp_increment;
                        sqlincrement.Parameters.AddWithValue("@量具編號", delete);
                        //sqlincrement.Parameters.AddWithValue("@量具狀態", delete1);
                        sqlincrement.Parameters.AddWithValue("@n", int.Parse(txt數量.Text));
                        sqlincrement.ExecuteNonQuery();

                        //string dele = "Select 數量 from 領用";

                        MessageBox.Show("送檢成功");
                        con.Close();
                        this.Close();
                        frmSongjianjilu frm = new frmSongjianjilu(b量具Search, b量具Insert, b量具Edit, b量具Del);
                        frm.ShowDialog();
                        this.Close();
                    }
                    else return;
                    }

                }
                if (Songjian == "2")
                {
                    //==============================================================================
                    if (txt數量.Text == "")
                    {
                        MessageBox.Show("請您輸入送檢數量", "錯誤", MessageBoxButtons.OK);
                        txt數量.Focus();
                        return;
                    }
                    int check = int.Parse(txt數量.Text);
                    if (check <= 0)
                    {
                        MessageBox.Show("不能返庫數量<=0", "錯誤", MessageBoxButtons.OK);
                        txt數量.Focus();
                        return;
                    }
                    else
                    {
                        // kiem tra so luong tra nho hon so luong ling yong
                        SqlCommand sqlcmd = new SqlCommand();
                        sqlcmd.Connection = con;
                        con.Open();
                        sqlcmd.CommandText = "select 數量 from 送檢 where 項次=" + "N'" + tbs0 + "'";
                        sqlcmd.CommandType = CommandType.Text;
                        var icount = sqlcmd.ExecuteScalar();
                        int inumdel;
                        bool k = int.TryParse(txt數量.Text, out inumdel);
                        int istocknumber = int.Parse(icount.ToString());
                        if (!k || inumdel > int.Parse(icount.ToString()))
                        {
                            MessageBox.Show("請您輸入送檢數量量具要刪除(int,<" + istocknumber + ")", "錯誤", MessageBoxButtons.OK);
                            txt數量.Focus();
                            return;
                        }
                        if (inumdel == istocknumber)//delete from database
                        {
                            DialogResult answer1 = MessageBox.Show("您確定要出入" + inumdel + "送檢量具碼", "警報", MessageBoxButtons.YesNo);
                            if (answer1 == DialogResult.Yes)
                            {
                            string delete = tbs0;
                            //DialogResult answer2 = MessageBox.Show("Bạn có muốn xóa A trong 修理紀錄 khi trả về 5 量具 va số lượng còn lại la bằng 0, hay ko ？", "警報", MessageBoxButtons.YesNo);
                            DialogResult answer2 = MessageBox.Show("當歸還" + inumdel + "量具及存送檢紀錄的量具剩余數量是0的，要不要刪除這個量具？", "警報", MessageBoxButtons.YesNo);
                                if(answer2 == DialogResult.Yes){
                                //con.Open();
                                string sp_delete = "sp_送檢_刪除";
                                SqlCommand sqldelete = new SqlCommand();
                                sqldelete.Connection = con;
                                sqldelete.CommandType = CommandType.StoredProcedure;
                                sqldelete.CommandText = sp_delete;
                                sqldelete.Parameters.AddWithValue("@項次", delete);
                                //MessageBox.Show("刪除成功");
                                sqldelete.ExecuteNonQuery();
                                }
                                if (answer2 == DialogResult.No)
                                {
                                    //string delete1 = tbl6;
                                    string sp_increment = "sp_送檢_更新_-n"; // tru ling yong
                                    SqlCommand sqlincrement = new SqlCommand();
                                    sqlincrement.Connection = con;
                                    sqlincrement.CommandType = CommandType.StoredProcedure;
                                    sqlincrement.CommandText = sp_increment;
                                    sqlincrement.Parameters.AddWithValue("@項次", delete);
                                    //sqlincrement.Parameters.AddWithValue("@量具狀態", delete1);
                                    sqlincrement.Parameters.AddWithValue("@n", int.Parse(txt數量.Text));
                                    sqlincrement.ExecuteNonQuery();
                                }
                                //Tra ve
                                string sp_update = "sp_量具訊息_更新_+n"; // cong vao kho
                                SqlCommand cmd1 = new SqlCommand(sp_update, con);
                                cmd1.CommandType = CommandType.StoredProcedure;
                                cmd1.Parameters.AddWithValue("@量具編號", txt_量具編號.Text);
                                //cmd1.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text.ToString());
                                cmd1.Parameters.AddWithValue("@n", txt數量.Text);
                                cmd1.ExecuteNonQuery();
                                MessageBox.Show("檢定成功");
                                this.Close();
                                con.Close();
                            }
                            else
                            {
                                return;
                            }
                        }
                        else
                        {
                            //====================================================================================
                            DialogResult answer = MessageBox.Show("您確定要出入" + int.Parse(txt數量.Text) + "量具嗎？", "警報", MessageBoxButtons.YesNo);
                            if (answer == DialogResult.Yes)
                            {
                                //con.Open();
                                //tra ve
                                string delete = tbs0;
                                //string delete1 = tbl6;
                                string sp_increment = "sp_送檢_更新_-n";// tru ling yong
                                SqlCommand sqlincrement = new SqlCommand();
                                sqlincrement.Connection = con;
                                sqlincrement.CommandType = CommandType.StoredProcedure;
                                sqlincrement.CommandText = sp_increment;
                                sqlincrement.Parameters.AddWithValue("@項次", delete);
                                //sqlincrement.Parameters.AddWithValue("@量具狀態", delete1);
                                sqlincrement.Parameters.AddWithValue("@n", int.Parse(txt數量.Text));
                                sqlincrement.ExecuteNonQuery();

                                //Tra ve
                                string sp_update = "sp_量具訊息_更新_+n"; // cong vao kho
                                SqlCommand cmd1 = new SqlCommand(sp_update, con);
                                cmd1.CommandType = CommandType.StoredProcedure;
                                cmd1.Parameters.AddWithValue("@量具編號", txt_量具編號.Text);
                                //cmd1.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text.ToString());
                                cmd1.Parameters.AddWithValue("@n", txt數量.Text);
                                cmd1.ExecuteNonQuery();
                                MessageBox.Show("歸還成功");
                                con.Close();
                                this.Close();
                            }
                            else return;
                        }
                    }
                }
            }
            catch (Exception ex) { con.Close(); MessageBox.Show(ex.Message); }
            finally { con.Close(); }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn修理紀錄_Click(object sender, EventArgs e)
        {
            frmSongjianjilu frm = new frmSongjianjilu(b量具Search, b量具Insert, b量具Edit, b量具Del);
            frm.ShowDialog();
        }

        private void txt_檢定機構_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 檢定機構 where 檢定機構代號=N'{0}'", txt_檢定機構.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName檢定機構.Text = "";
                else lbName檢定機構.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btn辦理人員Search_Click(object sender, EventArgs e)
        {
            frmSearchBanliRenYuan frm = new frmSearchBanliRenYuan(txt_辦理人員);
            frm.ShowDialog();
        }

        private void txt_辦理人員_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 辦理人員 where 人員代號=N'{0}'", txt_辦理人員.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName辦理人員.Text = "";
                else lbName辦理人員.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnSearch檢定機構_Click(object sender, EventArgs e)
        {
            frmSearchJiandingJigou frm = new frmSearchJiandingJigou(txt_檢定機構);
            frm.ShowDialog();
        }

        private void txt數量_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) || e.KeyChar == '.')
            {
                e.Handled = true;
                MessageBox.Show("請您輸入號碼", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txt_量具編號_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 量具訊息 where 量具編號=N'{0}'", txt_量具編號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName量具編號.Text = "";
                else lbName量具編號.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt廠牌_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 廠牌訊息 where 廠牌代號=N'{0}'", txt廠牌.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName廠牌.Text = "";
                else lbName廠牌.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt校正類別_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 校正類別訊息 where 校正類別代號=N'{0}'", txt校正類別.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName校正類別.Text = "";
                else lbName校正類別.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        
    }
}
